﻿/// <summary>
/// Summary description for utilities
/// </summary>
[System.Web.Services.WebService(Namespace = "http://exeterfriendly.com/")]
[System.Web.Services.WebServiceBinding(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class utilities : System.Web.Services.WebService
{

	public utilities()
	{
	}

	/// ToXmlDocument
	private static System.Xml.XmlDocument ToXmlDocument(System.Object data)
	{
		System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
		xmlDoc.LoadXml(EFS.Common.Serialization.Functions.SerializeToString(data));
		return xmlDoc;
	}

	#region Surveys

	//get all surveys
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Survey_GetById(System.Int32 id)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo surveyMsg = EFS.RC.CC.CH.Tables.Surveys.GetByID(id);
			return ToXmlDocument(surveyMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get all surveys
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Survey_GetAll(
		System.Int32? template,
		System.Int32? type,
		System.Int32? reviewType)
	{
		// log test
		EFS.EventLog.Log.LogInfo("Survey_GetAll entry", null);

		try
		{
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] surveyMsg = EFS.RC.CC.CH.Tables.Surveys.GetAll(template, (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType?)type, (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType?)reviewType);
			return ToXmlDocument(surveyMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get all available surveys
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Survey_GetAllAvailableConversationSurveysForUser(
		System.Guid conversationGuid,
		System.Int32? reviewType)
	{
		// log test
		EFS.EventLog.Log.LogInfo("Survey_GetAvailableConversationSurveysForUser entry", null);

		try
		{
			// get username
			System.String account = System.Web.HttpContext.Current.User.Identity.Name;
			System.String username = account.Split("\\".ToCharArray())[1];

			// get
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] surveyMsg =
				EFS.RC.CC.CH.Tables.Surveys.GetAvailableConversationSurveysForUser(
				username,
				conversationGuid,
				(EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType?)reviewType);
			return ToXmlDocument(surveyMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get all available surveys
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Survey_GetAllAvailableDocumentsSurveysForUser(
		System.String documentId,
		System.Int32? reviewType)
	{
		// log test
		EFS.EventLog.Log.LogInfo("Survey_GetAllAvailableDocumentsSurveysForUser entry", null);

		try
		{
			// get username
			System.String account = System.Web.HttpContext.Current.User.Identity.Name;
			System.String username = account.Split("\\".ToCharArray())[1];

			// get
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] surveyMsg =
				EFS.RC.CC.CH.Tables.Surveys.GetAvailableDocumentsSurveysForUser(
					username,
					documentId,
					(EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType?)reviewType);
			return ToXmlDocument(surveyMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//create skill
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Survey_GetAllConversationSurveySessions(
		System.Guid conversationGuid,
		System.Int32? reviewType)
	{
		// log test
		EFS.EventLog.Log.LogInfo("Survey_GetAllConversationSurveySessions entry", null);

		try
		{
			// get username
			System.String account = System.Web.HttpContext.Current.User.Identity.Name;
			System.String username = account.Split("\\".ToCharArray())[1];

			// get
			EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[] surveyMsg =
				EFS.RC.CC.CH.Tables.SurveySessionInfos.GetAllConversationSurveySessions(
					conversationGuid,
					username,
					(EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType?)reviewType);

			return ToXmlDocument(surveyMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//create skill
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument Survey_GetAllDocumentSurveySessions(
		System.String documentId,
		System.Int32? reviewType)
	{
		// log test
		EFS.EventLog.Log.LogInfo("Survey_GetAllDocumentSurveySessions entry", null);

		try
		{
			// get username
			System.String account = System.Web.HttpContext.Current.User.Identity.Name;
			System.String username = account.Split("\\".ToCharArray())[1];

			// ok
			EFS.RC.CC.CH.Entities.Surveys.Sessions.SessionInfo[] surveyMsg =
				EFS.RC.CC.CH.Tables.SurveySessionInfos.GetAllDocumentSurveySessions(
					documentId,
					username,
					(EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType?)reviewType);

			return ToXmlDocument(surveyMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get all surveyCategorys
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveyCategory_GetBySurveyID(System.Int32 surveyId)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Category[] surveyCategoryMsg = EFS.RC.CC.CH.Tables.SurveyCategories.GetBySurveyID(surveyId);
			return ToXmlDocument(surveyCategoryMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get all surveyQuestions
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveyQuestion_GetByCategoryID(System.Int32 categoryId)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Question[] surveyQuestionMsg = EFS.RC.CC.CH.Tables.SurveyQuestions.GetByCategoryId(categoryId);
			return ToXmlDocument(surveyQuestionMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get all surveyAnswers
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveyAnswer_GetByQuesionID(System.Int32 quesionId)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Answer[] surveyAnswerMsg = EFS.RC.CC.CH.Tables.SurveyAnswers.GetByQuestionID(quesionId);
			return ToXmlDocument(surveyAnswerMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//create skill
	[System.Web.Services.WebMethod]
	public System.Int32? SurveySessionData_Create(System.String sessionDataXML)
	{
		try
		{
			// get session data
			EFS.RC.Survey.Tools.Sessions.SessionData sessionData =
				EFS.Common.Serialization.Functions.Deserialize<EFS.RC.Survey.Tools.Sessions.SessionData>(sessionDataXML);

			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// begin
				connection.BeginTransaction();

				// get username
				System.String account = System.Web.HttpContext.Current.User.Identity.Name;
				System.String username = account.Split("\\".ToCharArray())[1];

				// is completed
				System.Int32 isCompleted = 0;

				// number of answers
				System.Int32 answers = sessionData.Answers.Length;
				System.Int32 questions = EFS.RC.CC.CH.Tables.Surveys.GetNumberOfQuestions(sessionData.Session.SurveyId.Value);

				if (answers == questions)
				{
					isCompleted = 1;
				}

				// create survey session
				System.Int32? surveySessionId = EFS.RC.CC.CH.Tables.SurveySessions.Create(
					sessionData.Session.SurveyId.Value,
					sessionData.Session.ConversationGuid,
					sessionData.Session.DocumentId,
					sessionData.Session.Survey_Stop,
					sessionData.Session.Survey_Start,
					sessionData.Session.Survey_Continue,
					username,
					isCompleted,
					connection);

				// create answers
				foreach (EFS.RC.CC.CH.Entities.Surveys.Sessions.Answer answer in sessionData.Answers)
				{
					if (answer != null)
					{
						EFS.RC.CC.CH.Tables.SurveySessionAnswers.Create(
							surveySessionId.Value,
							answer.SurveyAnswerId.Value,
							answer.Comment,
							connection);
					}
				}

				// begin
				connection.CommitTransaction();

				// ok
				return surveySessionId;
			}
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//create skill
	[System.Web.Services.WebMethod]
	public System.Int32? SurveySessionData_Update(System.String sessionDataXML)
	{
		try
		{

			EFS.RC.Survey.Tools.Sessions.SessionData sessionData =
				EFS.Common.Serialization.Functions.Deserialize<EFS.RC.Survey.Tools.Sessions.SessionData>(sessionDataXML);

			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// begin
				connection.BeginTransaction();

				// get username
				System.String account = System.Web.HttpContext.Current.User.Identity.Name;
				System.String username = account.Split("\\".ToCharArray())[1];

				// is completed
				System.Int32 isCompleted = 0;

				// number of answers
				System.Int32 answers = sessionData.Answers.Length;
				System.Int32 questions = EFS.RC.CC.CH.Tables.Surveys.GetNumberOfQuestions(sessionData.Session.SurveyId.Value);

				if (answers == questions)
				{
					isCompleted = 1;
				}

				// create survey session
				EFS.RC.CC.CH.Tables.SurveySessions.Update(
					sessionData.Session.ID.Value,
					sessionData.Session.Survey_Stop,
					sessionData.Session.Survey_Start,
					sessionData.Session.Survey_Continue,
					isCompleted,
					connection);

				// create answers
				foreach (EFS.RC.CC.CH.Entities.Surveys.Sessions.Answer answer in sessionData.Answers)
				{
					if (answer != null)
					{
						// get existing answer
						EFS.RC.CC.CH.Entities.Surveys.Sessions.AnswerInfo[] answerExist = null;
						answerExist = EFS.RC.CC.CH.Tables.SurveySessionAnswersInfos.GetAnswerByQuestionId(
							sessionData.Session.ID.Value,
							answer.SurveyAnswerId.Value,
							connection);

						if (answerExist.Length > 0)
						{
							EFS.RC.CC.CH.Tables.SurveySessionAnswers.Update(
							answer.SurveyAnswerId.Value,
							answer.Comment,
							answerExist[0].Answer.ID.Value,
							connection);
						}
						else
						{
							EFS.RC.CC.CH.Tables.SurveySessionAnswers.Create(
								sessionData.Session.ID.Value,
								answer.SurveyAnswerId.Value,
								answer.Comment,
								connection);
						}
					}
				}

				// begin
				connection.CommitTransaction();

				// ok
				return 0;
			}
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	//get all surveyQuestions
	[System.Web.Services.WebMethod]
	public System.Xml.XmlDocument SurveySession_GetAllAnswers(System.Int32 surveySessionId)
	{
		try
		{
			EFS.RC.CC.CH.Entities.Surveys.Sessions.AnswerInfo[] surveyQuestionMsg = EFS.RC.CC.CH.Tables.SurveySessionAnswersInfos.GetAllAnswers(surveySessionId);
			return ToXmlDocument(surveyQuestionMsg);
		}
		catch (System.Exception ex)
		{
			// log
			EFS.EventLog.Log.LogException(ex, null);

			// throw
			throw ex;
		}
	}

	#endregion

}